package com.yimi.yinhepda.views.dispatch_module;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.bean.AddEntity;
import com.yimi.yinhepda.entity.bean.DispatchBackScanChildEntity;
import com.yimi.yinhepda.entity.bean.DispatchBackScanEntity;
import com.pda.ym.utils.DateHelper;
import com.pda.ym.utils.SoundVibratorManager;
import com.pda.ym.utils.StringUtils;
import com.yimi.yinhepda.base.BaseInitActivity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;

/**
 * 配送退单添加界面
 *
 * @author 000432
 * @date 2018/3/8 0008
 */
public class DispatchBackScanAddActivity extends BaseInitActivity {

    /**
     * 运单数据框
     */
    @Bind(R.id.back_scan_add_input)
    EditText maddInput;
    /**
     * 添加按钮
     */
    @Bind(R.id.back_scan_add_btn)
    Button mAddBtn;
    /**
     * 数据列表
     */
    @Bind(R.id.back_scan_add_listview)
    ListView mListView;
    @Bind(R.id.back_scan_back)
    Button mBackBtn;
    @Bind(R.id.back_scan_ok)
    Button mOkBtn;
    @Bind(R.id.show_add_count)
    TextView mAddCount;
    private List<DispatchBackScanEntity> mScanList;
    private List<AddEntity> dataList = new ArrayList<>();
    private MyAdapter mMyAdapter;

    @Override
    public String getPageName() {
        return "手动添加单号";
    }

    public DispatchBackScanAddActivity() {
        super(true);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_add_dispatch_back_scan;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initEvent() {
        mAddBtn.setOnClickListener(this);
        mBackBtn.setOnClickListener(this);
        mOkBtn.setOnClickListener(this);
    }

    @Override
    public void initData() {
        ((TextView) findViewById(R.id.title)).setText("手动添加单号");
        findViewById(R.id.num).setVisibility(View.GONE);

        mScanList = DispatchBackScanActivity.mScanList;
        mMyAdapter = new MyAdapter(this, dataList);
        mListView.setAdapter(mMyAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scan_add_btn:
                doScan(maddInput.getText().toString().trim());
                break;
            case R.id.back_scan_back:
            case R.id.back_scan_ok:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void handleBarCode(String result) {
        if (result == null) {
            SoundVibratorManager.playSound(2);
            showToast("运单号扫描失败");
        } else {
            maddInput.setText(result);
            doScan(result);
        }
    }

    /**
     * 处理扫描
     */
    protected void doScan(String result) {
        if (!StringUtils.isWaybillNo(result) && !StringUtils.isPackgeNo(result)) {
            SoundVibratorManager.playSound(2);
            showToast("单号或包号不规范!");
            return;
        }
        //遍历子单号是否在列表中
        boolean inChildList = false;
        for (DispatchBackScanEntity item : mScanList) {
            for (DispatchBackScanChildEntity childItem : item.getChildEntityList()) {
                if (result.equals(childItem.getChildWaybillNo())) {
                    inChildList = true;
                }
            }
        }
        if (inChildList) {
            SoundVibratorManager.playSound(2);
            showToast("运单号已扫描！");
            return;
        }
        //遍历主单号是否在列表中
        DispatchBackScanEntity scanEntity = null;
        boolean inList = false;
        String waybillNo = result.substring(0, 12);
        for (DispatchBackScanEntity item : mScanList) {
            if (waybillNo.equals(item.getWayBillNo())) {
                scanEntity = item;
                inList = true;
            }
        }
        //不存在列表中，将扫描的运单加入到列表中
        if (!inList) {
            scanEntity = new DispatchBackScanEntity();
            scanEntity.setWayBillNo(waybillNo);
            scanEntity.setScanTime(DateHelper.getDateFormateOffline());
            scanEntity.setChildEntityList(new ArrayList<DispatchBackScanChildEntity>());
            mScanList.add(scanEntity);
        }
        DispatchBackScanChildEntity childEntity = new DispatchBackScanChildEntity();
        childEntity.setWaybillNo(waybillNo);
        childEntity.setChildWaybillNo(result);
        scanEntity.getChildEntityList().add(childEntity);
        scanEntity.setScanNum(scanEntity.getChildEntityList().size());

        SoundVibratorManager.playSound(1);

        //更新显示信息
        dataList.add(new AddEntity(waybillNo, result));
        mMyAdapter.notifyDataSetChanged();

        //设置总票，总件数
        if (dataList != null && dataList.size() > 0) {
            Set<String> set = new HashSet<String>();
            for (AddEntity item : dataList) {
                set.add(item.getWaybillNum());
            }
            mAddCount.setText(set.size() + "/" + dataList.size());
        }
    }

    static class MyAdapter extends BaseAdapter {
        private Context context;
        private List<AddEntity> dataList;

        public MyAdapter(Context context, List<AddEntity> dataList) {
            this.context = context;
            this.dataList = dataList;
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHolder mHolder;
            if (convertView == null) {
                mHolder = new MyHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.dispatch_back_scan_add_item, null);
                mHolder.sequencyNum =  convertView.findViewById(R.id.tv_serialNum);
                mHolder.childwayBillNum =  convertView.findViewById(R.id.tv_childWayBill);
                convertView.setTag(mHolder);
            } else {
                mHolder = (MyHolder) convertView.getTag();
            }
            mHolder.sequencyNum.setText(position + 1 + "");
            mHolder.childwayBillNum.setText(dataList.get(position).getChildwayBillNum());

            return convertView;
        }
    }

    static class MyHolder {
        TextView sequencyNum;
        TextView childwayBillNum;
    }
}
